import { defineStore } from 'pinia'
import request from '@/utils/request'
import { CategoryItem, ApiRes, TopCategory, SubCategory } from '@/types/data'
import { topCategory } from '../constants'
const useCategoryStore = defineStore('category', {
  state() {
    return {
      list: topCategory.map((item) => {
        return {
          name: item
        }
      }) as CategoryItem[],
      topCategorys: {} as TopCategory,
      subCategory: {} as SubCategory
    }
  },
  actions: {
    //获取所有分类信息
    async getAllCategory() {
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      this.list = res.data.result
    },
    //获取一级分类信息
    async getTopCategory(id: string) {
      const res = await request.get<ApiRes<TopCategory>>('/category', { params: { id } })
      this.topCategorys = res.data.result
    },
    //获取二级分类信息
    async getSubCategory(id: string) {
      const res = await request.get<ApiRes<SubCategory>>('/category/sub/filter', { params: { id } })
      this.subCategory = res.data.result
    }
  },
  getters: {}
})

export default useCategoryStore
